package com.wsn.smarthouse;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//import java.net.SocketException;
import java.nio.charset.Charset;
import java.net.UnknownHostException;
import java.io.IOException;

import android.util.Log;
//import com.wsn.util.Log;


/**
 * Send message to host use UTF-8 code
 * @title NetSend
 * @author kong
 * @version 1.0
 * @see import java.net.DatagramSocket
 */


//Network Send Class
public class NetSend extends Thread{
	private DatagramSocket ds;
	private final String remoteHostAddress = "192.168.1.250";
	private final String remoteHostPort = "6789";
	private final String logtag = "netsend";
	private String cmd;
	
	public NetSend(DatagramSocket ds,String cmd){
		this.ds=ds;
		this.cmd=cmd;
	}
	
	@Override
	public void run(){
		try{
			if(ds!=null){
				byte[] sendData = cmd.getBytes(Charset.forName("UTF-8"));
				DatagramPacket sdp = new DatagramPacket(sendData,
						sendData.length,
						InetAddress.getByName(remoteHostAddress),
						Integer.parseInt(remoteHostPort));
				Log.v(logtag,cmd);
				ds.send(sdp);
			}
		}catch(UnknownHostException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}